<?php
/**
 * Created by PhpStorm.
 * User: t
 * Date: 2024/3/19
 * Time: 17:07
 */

namespace app\home\controller;

use app\home\model\Level;
use app\home\model\UserCoupon;
use app\merchant\model\GoodsClassify;
use app\merchant\model\GoodsDiscount;
use app\Request;


class Goods
{

    public function classifyList()
    {
        $goodsClassify = new GoodsClassify();
        $list = $goodsClassify->where('status', '=', 1)->order('sort', 'asc')->select()->toArray();
        return success('商品分类', $list);
    }

    public function goodsList(Request $request)
    {
        $param = $request->param();
        $where[] = ['status','=',1];
        if (isset($param['classify_id']) && !empty($param['classify_id'])) {
            $where[] = ['classify_id' ,'=',$param['classify_id']];
        }
        if (isset($param['name']) && !empty($param['name'])) {
            $where[] = ['name','like','%'.$param['name'].'%'];
        }
        $limit = 10;
        $start = 0;
        if (isset($param['limit']) && !empty($param['limit'])) {
            $limit = $param['limit'];
        }
        if (isset($param['page']) && !empty($param['page'])) {
            $start = ($param['page'] - 1) * $limit;
        }
        $goodsModel = new \app\merchant\model\Goods();
        $list = $goodsModel->where($where)
            ->field('id,name,host_img,desc,price,market_price,sales_volume,buying_pattern,inventory,classify_id')
            ->limit($start, $limit)
            ->select()->toArray();

        $count = $goodsModel->where($where)
            ->field('id,name,host_img,desc,price,market_price,sales_volume,buying_pattern,inventory,classify_id')
            ->count();
        if ($list) {
            $classify_id = array_column($list, 'classify_id');
            $couponModel = new \app\home\model\Coupon();
            $newUserCouponTime = 60 * 60 * 24 * 3;
            $couponWhere[] = ['status', '=', 1];
            $classify_id[] = 0;
            $couponWhere[] = ['classify_id', 'in', $classify_id];
            if (strtotime($request->userInfo['create_time']) + $newUserCouponTime < time()) {
                $couponWhere[] = ['type', 'in', [1, 2]];
            } else {
                $couponWhere[] = ['type', 'in', [1]];
            }
            $userCouponModel = new UserCoupon();
            $userNotUseCoupon = $userCouponModel->where(['uid' => $request->userInfo['id']])
                ->where('status', 'in', [2, 3])->select()->toArray();
            $couponId = [];
            if ($userNotUseCoupon) {
                $couponId = array_column($userNotUseCoupon, 'coupon_id');
            }
            $couponData = $couponModel->where($couponWhere)->select()->toArray();
            foreach ($list as $key => $value) {
                $list[$key]['coupon_list'] = [];
                foreach ($couponData as $couponDatum) {
                    if ($value['classify_id'] == $couponDatum['classify_id'] || $couponDatum['classify_id'] == 0) {
                        if (!in_array($couponDatum['id'], $couponId)) {
                            $list[$key]['coupon_list'][] = $couponDatum;
                        }
                    }
                }
            }
        }
        return success('商品列表', ['list' => $list, 'count' => $count]);

    }

    public function goodsInfo(Request $request)
    {
        $param = $request->param();
        $goodsModel = new \app\merchant\model\Goods();
        $info = $goodsModel->where(['id' => $param['id']])->find();
        $goodsDiscountModel = new GoodsDiscount();
        $info['discount_amount'] = 0;
        $info['discount_ratio'] = 0;
        $goodsDiscountInfo = $goodsDiscountModel->where(['level_id' => $request->userInfo['level_id'], 'goods_id' => $param['id']])->find();
        if (!empty($goodsDiscountInfo) && $goodsDiscountInfo['discount'] > 0) {  // 针对商品设置了 折扣
            $info['discount_amount'] = bcdiv(bcmul($info['price'], $goodsDiscountInfo['discount'], 2), 100, 2);
            $info['discount_ratio'] = $goodsDiscountInfo['discount'];
        } else {
            $levelModel = new Level();
            $levelInfo = $levelModel->where(['id' => $request->userInfo['level_id']])->find();
            if ($levelInfo['shopping_preferential'] > 0) { // 针对商城全局的折扣
                $info['discount_amount'] = bcdiv(bcmul($info['price'], $levelInfo['shopping_preferential'], 2), 100, 2);
                $info['discount_ratio'] = $levelInfo['shopping_preferential'];
            } else {
                if ($levelInfo['preferential'] > 0) {  // 全部是产品的折扣
                    $info['discount_amount'] = bcdiv(bcmul($info['price'], $levelInfo['preferential'], 2), 100, 2);
                    $info['discount_ratio'] = $levelInfo['preferential'];
                }
            }
        }
        return success('商品详情', $info);
    }

    public function GooodsDiscount(Request $request)
    {
        $param = $request->param();
        $goodsDiscountModel = new \app\home\model\GoodsDiscount();
        $discount = 0;
        $goodsDiscountInfo = $goodsDiscountModel->goodsLevelDiscount($request->userInfo['level_id'], $param['goods_id']);

        if (!empty($goodsDiscountInfo) && $goodsDiscountInfo['discount'] > 0) {  // 针对商品设置了 折扣
            $discount = $goodsDiscountInfo['discount'];
        } else {
            $levelModel = new Level();
            $levelInfo = $levelModel->where(['id' => $request->userInfo['level_id']])->find();

            if ($levelInfo['shopping_preferential'] > 0) { // 针对商城全局的折扣
                $discount = $levelInfo['shopping_preferential'];
            } else {
                if ($levelInfo['preferential'] > 0) {  // 全局的折扣
                    $discount = $levelInfo['preferential'];
                }
            }
        }
        return success('商品详情', ['goods_id' => $param['goods_id'], 'discount' => $discount]);
    }


}
